package com.geepi.soundcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.geepi.soundcloud.model.BaseResponse;
import com.geepi.soundcloud.model.BaseResutlResponse;
import com.geepi.soundcloud.model.User;
import com.geepi.soundcloud.service.IUserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
public class LoginController {
    @Autowired
	private IUserService userService;
	
    private static Logger logger = LoggerFactory.getLogger(LoginController.class);
    
	@RequestMapping("/login")
	@ResponseBody
	public BaseResutlResponse login(@RequestParam("phoneId") String phoneId,
			                        @RequestParam("password") String password) {
		logger.info("cloud record...");
		User user = userService.getUserById(phoneId);
		BaseResutlResponse baseResutlResponse = new BaseResutlResponse();
		BaseResponse baseResponse = new BaseResponse();
		if (user != null) {
			baseResponse.setRc(0);
			baseResponse.setRd("login success");
			baseResutlResponse.setResponse(user);
		} else {
			baseResponse.setRc(1);
			baseResponse.setRd("password is error");
			logger.info("password is erro");;
		}
		baseResutlResponse.setResult(baseResponse);

		return baseResutlResponse;
	}
}
